package com.project.servlet;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import com.project.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;

public class AddServlet extends HttpServlet {
    private IUserDao userDao=new UserDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求编码集，只对post有效
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //将表单数据封装成用户对象
        UserBean userBean=new UserBean();
        userBean.setName(req.getParameter("userName"));
        userBean.setPwd((req.getParameter("pwd")));
        userBean.setBirthday(LocalDate.parse(req.getParameter("birthday")));
        userBean.setPhone(req.getParameter("phone"));
        userBean.setMoney(Integer.parseInt(req.getParameter("money")));
        userDao.add(userBean);
        resp.getWriter().print("注册成功");
    }
}
